package cn.zc.spring.boot.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @Author: zc
 * @Description: kafka 消费者
 * @Date: 2019/06/28 10:26
 */
@Component
public class Consumer {

    @KafkaListener(topics = {"springboot_kafka_demo_201906281024"}, groupId = "test-201907021657")
    public void consume(ConsumerRecord<?, ?> record){

        Optional<?> kafkaMessage = Optional.ofNullable(record.value());

        if (kafkaMessage.isPresent()) {

            Object message = kafkaMessage.get();
            System.out.println("1---->"+record);
            System.out.println("1---->"+message);

        }

    }

    // 与consume消费相同Topic，但两个Consumer不是同一个Group
    @KafkaListener(topics = {"springboot_kafka_demo_201906281024"}, groupId = "test-201907021703")
    public void consume2(ConsumerRecord<?, ?> record){

        Optional<?> kafkaMessage = Optional.ofNullable(record.value());

        if (kafkaMessage.isPresent()) {

            Object message = kafkaMessage.get();
            System.out.println("2---->"+record);
            System.out.println("2---->"+message);

        }

    }

    // 与consume消费不同Topic，但两个Consumer是同一个Group
    @KafkaListener(topics = {"springboot_kafka_demo_201906281025"}, groupId = "test-201907021657")
    public void consume3(ConsumerRecord<?, ?> record){

        Optional<?> kafkaMessage = Optional.ofNullable(record.value());

        if (kafkaMessage.isPresent()) {

            Object message = kafkaMessage.get();
            System.out.println("3---->"+record);
            System.out.println("3---->"+message);

        }

    }
}
